



//
//  NRoleSelectedViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/20.
//  Copyright © 2018年 youedata. All rights reserved.
//
#import "AppDelegate.h"
#import "NRoleSelectedViewController.h"
#import "NRootViewController.h"
@interface NRoleSelectedViewController ()

@property (weak, nonatomic) IBOutlet UIButton *gerenBtn;
@property (weak, nonatomic) IBOutlet UIButton *enterPriseRoleBtn;
@property (weak, nonatomic) IBOutlet UIButton *govRoleBtn;

@end

@implementation NRoleSelectedViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    //背景蓝
    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"用户角色" textColor:kWhiteColor fontSize:18 itemType:center];
    
    // Do any additional setup after loading the view from its nib.
    
}
-(void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    
    self.enterPriseRoleBtn.layer.cornerRadius = kCornerRadius;
    self.enterPriseRoleBtn.layer.masksToBounds = YES;
    self.govRoleBtn.layer.cornerRadius = kCornerRadius;
    self.govRoleBtn.layer.masksToBounds = YES;
    
    self.gerenBtn.layer.cornerRadius = kCornerRadius;
    self.gerenBtn.layer.masksToBounds = YES;
}



#pragma mark - 选择企业和个人
// 0个人   1企业    2政府
//企业

- (IBAction)EnterPriseBtnAction:(UIButton *)sender {
    
    [self clearUserInfos];
//    [self logOut];
    //测试帐号 qiye03  111111
    ///tag   301  302   303
    if (sender.tag==301) {
        
        //企业
        //下面两个标记都可以用选一个就好
        [self markUserTypeWithType:@"1"];
        [NUserModel shareInstance].userInfoType = @"1";
        
    }else if (sender.tag==303){
        
        //个人
        //下面两个标记都可以用选一个就好
        [self markUserTypeWithType:@"0"];
        [NUserModel shareInstance].userInfoType = @"0";
    }
//    [self postNotification];
//    [self dismissViewControllerAnimated:YES completion:nil];
//    [self goIntoMainWindow];
    
}

#pragma mark - 政府端
//政府
- (IBAction)govBtnAction:(UIButton *)sender {
    
    [self clearUserInfos];
//    [self logOut];
    [self markUserTypeWithType:@"2"];
    [NUserModel shareInstance].userInfoType = @"2";
//    [self postNotification];
//    [self dismissViewControllerAnimated:YES completion:nil];
//    [self goIntoMainWindow];
    
}

//存储用户类型
- (void)markUserTypeWithType:(NSString *)type {
    
//    NSString *nType = [[NSUserDefaults standardUserDefaults]objectForKey:kUserType];
//
//    if (nType.length==1) {
//
//        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:kUserType];
//        [[NSUserDefaults standardUserDefaults]synchronize];
//
//    }
    NSString *oldType = [[NSUserDefaults standardUserDefaults]objectForKey:kUserType];
    [[NSUserDefaults standardUserDefaults]setObject:type forKey:kUserType];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSString *newType = [[NSUserDefaults standardUserDefaults]objectForKey:kUserType];
    NSLog(@"old:%@__new:%@",oldType,newType);
    
    //更换用户角色
    [self changeRole];
}

///更换角色
- (void)changeRole{
    
    //    [NUserModel shareInstance].isChangeRole =YES;
    [self logOut];
    [self postNotification];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self goIntoMainWindow];
    
}

- (void)logOut {
    [NUserModel shareInstance].isLogin = false;
}


///进入主页面
- (void)goIntoMainWindow {
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"fir"]) {

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"fir"];
        [UIView animateWithDuration:.3 animations:^{
            
            self.view.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            NRootViewController *tab = [[NRootViewController alloc]init];
            AppDelegate *appDelegate = (id)([UIApplication sharedApplication].delegate);
            appDelegate.window.rootViewController = tab;
            
        }];
       
    }else{
        
    }
    
}



/// 清空用户信息
- (void)clearUserInfos {
    
    [NUserModel cleanUserInfo];
    
}

-  (void)postNotification{
    [[NSNotificationCenter defaultCenter]postNotificationName:kRefreshNotification object:nil];
}


@end
